package org.myy.learn.hot.回溯;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

@Slf4j
public class 子集 {

    public static List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums.length == 0) {return result;}

        List<Integer> path = new ArrayList<>();
        backTrack(nums, 0, path, result);

        return result;
    }

    private static void backTrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) {
        // Add current path to result (all valid subsets)
        log.info("path={}",path);
        result.add(new ArrayList<>(path));

        for (int i = start; i < nums.length; i++) {
            path.add(nums[i]);
            backTrack(nums, i + 1, path, result);  // ToDO 基于i开始，而不是start开始
            path.remove(path.size() - 1);  // Remove last element (backtrack)
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3};
        List<List<Integer>> result = subsets(nums);
        System.out.println("执行结束，子集=" + result);
        // Output: 执行结束，子集=[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
    }
}
